package Conexion;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class Conexion {
	
	private static Connection conexion1;
	private static Connection conexion2;
	private static String bdconexion1;
	private static String bdconexion2;
	private static String schemaConexion1;
	private static String schemaConexion2;
	
	private static Connection conexion(String driver, String url, String username, String password) {
		Connection conexion = null;
		// Load database driver if not already loaded.
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// Establish network connection to database.
		try {
			conexion = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conexion;
	}
	
	public static void comenzarConexion() {
		String dirActual = System.getProperty("user.dir");
		String dirArchivo = dirActual +"/usuario1.properties";
		Properties file = new Properties();
		FileInputStream in;
		try {
			in = new FileInputStream(dirArchivo);
			file.load(in);
			in.close();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		String driver1 = file.getProperty("driver");
		String url1 = file.getProperty("url") + file.getProperty("host") + file.getProperty("db");
		String username1 = file.getProperty("usuario");
		String password1 = file.getProperty("password");
		bdconexion1 = file.getProperty("db");
		schemaConexion1 = file.getProperty("schema");
		conexion1 = conexion(driver1, url1, username1, password1);
		dirArchivo = dirActual + "/usuario2.properties";
		try {
			in = new FileInputStream(dirArchivo);
			file.load(in);
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String driver2 = file.getProperty("driver");
		String url2 = file.getProperty("url") + file.getProperty("host") + file.getProperty("db");
		String username2 = file.getProperty("usuario");
		String password2 = file.getProperty("password");
		bdconexion2 = file.getProperty("db");
		schemaConexion2 = file.getProperty("schema");
		conexion2 = conexion(driver2, url2, username2, password2);
	}

	public static Connection getConexion1() {
		return conexion1;
	}

	public static Connection getConexion2() {
		return conexion2;
	}
	
	public static String getBDConexion1(){
		return bdconexion1;
	}
	
	public static String getBDConexion2(){
		return bdconexion2;
	}
	
	public static String getSchema1(){
		return schemaConexion1;
	}
	
	public static String getSchema2(){
		return schemaConexion2;
	}
}
